package com.hsy.crm.web.service.asyn;

import org.springframework.stereotype.Service;

@Service
public class AsynStoreOutServcie{/*

    @Autowired
    StoreProductDetailMapper storeProductDetailMapper;

    @Autowired
    AsynStoreInServcie asynStoreInServcie;

    *//**
     * 出库利润计算
     *
     * @param message
     * @param storeProductDetail
     *//*
    public void init(Message message, StoreProductDetail storeProductDetail){

        *//**
         * 该明细是否出过库
         * [采购 -- 销售 -- 采购单反审核]
         *
         * [采购 -- 销售 -- 采购退货（未指定采购单）]
         * [采购 -- 销售 -- 采购退货（指定了采购单）]
         *
         * [采购 -- 销售 -- 销售退货反审核（未指定销售单）]
         * [采购 -- 销售 -- 销售退货反审核（指定了销售单）]
         *//*
        StoreProductDetail repeatStoreProductDetail = this.selectRepeatSortDetail(storeProductDetail);

        if( repeatStoreProductDetail == null ){
            this.calcStoreProductDetailDone(message,storeProductDetail);
        }else {
            //重置所有明细（该主库存）利润处理为最初始状态
            StoreProductDetail clear = new StoreProductDetail();
            clear.setCustomerId(message.getCustomerId());
            clear.setStoreProductId(repeatStoreProductDetail.getStoreProductId());
            storeProductDetailMapper.clearOut(clear);

            // 使本次数据 和 重复数据 不再参与利润处理
            this.updateRushBack(storeProductDetail,repeatStoreProductDetail);


            this.clearOut(repeatStoreProductDetail);
            this.calcStoreProductDetailDone(message,storeProductDetail,repeatStoreProductDetail);

            // 处理 storeProductId相同的 未处理利润的明细
            int sumStockNumber = storeProductDetailMapper.sumStockNumber(message);
            asynStoreInServcie.unCalc(message,storeProductDetail.getStoreProductId(),sumStockNumber);
        }
    }

    public void calcStoreProductDetailDone(Message message,StoreProductDetail currStoreProductDetail){
        this.calcStoreProductDetail(message,currStoreProductDetail,null);
    }

    public void calcStoreProductDetailDone(Message message,StoreProductDetail currStoreProductDetail,StoreProductDetail firstStoreProductDetail){
        this.calcStoreProductDetail(message,currStoreProductDetail,firstStoreProductDetail);
    }

    private void updateRushBack(StoreProductDetail currStoreProductDetail,StoreProductDetail firstStoreProductDetail){
        currStoreProductDetail.setRushBack(App.Booleans.YES);
        storeProductDetailMapper.updateRushBack(currStoreProductDetail);

        firstStoreProductDetail.setRushBack(App.Booleans.YES);
        firstStoreProductDetail.setCustomerId(currStoreProductDetail.getCustomerId());
        storeProductDetailMapper.updateRushBack(firstStoreProductDetail);
    }

    *//**
     * 处理具体的某一个明细的利润情况
     * @param message
     * @param currStoreProductDetail    待处理的明细
     * @param firstStoreProductDetail   指定以该明细库存算利润
     *//*
    private void calcStoreProductDetail(Message message,StoreProductDetail currStoreProductDetail,StoreProductDetail firstStoreProductDetail){

        //未指定以哪条明细算利润时  查询最早一次库存明细
        if( firstStoreProductDetail == null ){
            firstStoreProductDetail = this.getFirstStoreProductDetail(message,currStoreProductDetail);
        }

        // 无库存时
        if( firstStoreProductDetail == null || firstStoreProductDetail.getId() == null ){
            this.noStock(currStoreProductDetail);
            storeProductDetailMapper.addOutTmp(currStoreProductDetail);
            return;
        }

        firstStoreProductDetail.setCustomerId(message.getCustomerId());

        //属性获取 获取本次出库数量和成本
        CostResult costResult = getCostResult(firstStoreProductDetail,currStoreProductDetail);

        //属性设置 设置sort 起始sort无值则设置为起始  起始sort有值则设置为结束
        this.setCurrSort(currStoreProductDetail,firstStoreProductDetail);

        //属性设置  设置本次 出库总数量 出库总金额
        this.setCurrStoreProductDetail(currStoreProductDetail,costResult);

        //数据更新  out_number out_price  起止sort
        storeProductDetailMapper.addOut(currStoreProductDetail);

        //数据更新  out_number out_price  起止sort
        this.setFirstStoreProducctDetail(firstStoreProductDetail,costResult);
        this.setFirstSort(currStoreProductDetail,firstStoreProductDetail);
        storeProductDetailMapper.addOut(firstStoreProductDetail);

        if( costResult.isDone() ){
            //处理完成 清理临时数据
            storeProductDetailMapper.clearTmp(currStoreProductDetail);
        }else{
            //未处理完成则继续
            calcStoreProductDetail(message,currStoreProductDetail,null);
        }

    }

    private void noStock(StoreProductDetail currStoreProductDetail){
        ProductCostPo productCost = storeProductDetailMapper.selectLastSaleCost(currStoreProductDetail);
        Long cost = -2L;
        if( productCost != null ){

            StoreProductDetail tmpStoreProductDetail = storeProductDetailMapper.selectByPrimaryKey(currStoreProductDetail);
            cost = CalcUtils.div(productCost.getStockTotalPrice(),productCost.getStockTotalNumber());
            Long number = tmpStoreProductDetail.getNumber() - tmpStoreProductDetail.getOutNumber();
            cost = CalcUtils.mul(number,cost);
        }

        currStoreProductDetail.setTmpPrice(cost.longValue());
    }

    private CostResult getCostResult(StoreProductDetail firstStoreProductDetail, StoreProductDetail currStoreProductDetail){
        DivResult result = CalcUtils.result(firstStoreProductDetail.getNumber(),firstStoreProductDetail.getPrice());
        Long cost; Long number;boolean done;

        long firstElseNumber = firstStoreProductDetail.getNumber() - firstStoreProductDetail.getOutNumber();
        long currElseNumber = currStoreProductDetail.getNumber() - currStoreProductDetail.getOutNumber();

        if( firstElseNumber > currElseNumber ){
            number = currElseNumber;
            cost = CalcUtils.mul(result.getSmall(),number);
            cost = CalcUtils.div100(cost);
            done = true;
        }else{
            number = firstElseNumber == currElseNumber ? currElseNumber : firstElseNumber;
            if( result.getStrong() == 0 ){
                cost = CalcUtils.mul(result.getSmall(),number);
                cost = CalcUtils.div100(cost);
            }else{
                cost = CalcUtils.mul(result.getSmall(),(Long)(number-App.DECIMAL.DIV));
                cost = CalcUtils.div100(cost);
                cost = CalcUtils.add(cost,result.getStrong());
            }
            done = firstElseNumber == currElseNumber;
        }
        return new CostResult(number,cost,done);
    }

    private StoreProductDetail selectRepeatSortDetail(StoreProductDetail storeProductDetail){
        return storeProductDetailMapper.selectRepeatSortDetail(storeProductDetail);
    }

    private void setCurrStoreProductDetail(StoreProductDetail currStoreProductDetail,CostResult costResult){
        currStoreProductDetail.setOutNumber(costResult.getNumber());
        currStoreProductDetail.setOutPrice(costResult.getCost());
    }

    private void setFirstStoreProducctDetail(StoreProductDetail firstStoreProductDetail,CostResult costResult){
        firstStoreProductDetail.setOutNumber(costResult.getNumber());
        firstStoreProductDetail.setOutPrice(costResult.getCost());
    }

    private void clearOut(StoreProductDetail storeProductDetail){
        storeProductDetail.setOutNumber(0L);
        storeProductDetail.setOutPrice(0L);
        storeProductDetail.setRelationStartSort(-1L);
        storeProductDetail.setRelationEndSort(-1L);
    }

    private void setCurrSort(StoreProductDetail currStoreProductDetail,StoreProductDetail firstStoreProductDetail){
        if( currStoreProductDetail.getRelationStartSort() == -1 ){
            currStoreProductDetail.setRelationStartSort(firstStoreProductDetail.getSort());
        }else{
            currStoreProductDetail.setRelationEndSort(firstStoreProductDetail.getSort());
        }
    }

    private void setFirstSort(StoreProductDetail currStoreProductDetail,StoreProductDetail firstStoreProductDetail){
        if( firstStoreProductDetail.getRelationStartSort() == -1 ){
            firstStoreProductDetail.setRelationStartSort(currStoreProductDetail.getSort());
        }else{
            firstStoreProductDetail.setRelationEndSort(currStoreProductDetail.getSort());
        }
    }

    private StoreProductDetail getFirstStoreProductDetail(Message message,StoreProductDetail currStoreProductDetail){
        StoreProductDetail storeProductDetail = new StoreProductDetail();
        storeProductDetail.setStoreProductId(currStoreProductDetail.getStoreProductId());
        storeProductDetail.setCustomerId(message.getCustomerId());
        return storeProductDetailMapper.getFirstStoreProductDetail(storeProductDetail);
    }
*/
}